#!/usr/bin/perl
# make a soft link to /usr/lib/cgi-bin/ to add it to the apache
use strict;
use CGI;
use CGI qw/:standard/;
use CGI::Pretty qw/:standard *-table *-div/;
use Time::Local;
use FindBin '$Bin';
use lib $Bin;
use DBI;
use Data::Dumper;
use CgiUtil;


sub main {
	my $stock = url_param('stock');
	my $order_by = url_param('order_by');
	my $dir = url_param('dir');

	print myhead("认购证查询:" . $stock ."/". $order_by ."/". $dir);
	print h3("认购证查询:" . $stock ."/". $order_by ."/". $dir);

	if (length($stock)<1) {
		my @file_list = `ls /data/share/code/trunk/perl/stock/USA/warrant/best_warrant/data/res/`;
		foreach my $file (@file_list) {
			if ($file =~ /(.+?)\.txt/) {
				$file = $1;
				print "<a href=\"/cgi-bin/warrant.cgi?stock=$file&order_by=1&dir=asc\" target=\"_blank\">$file</a><br>" ;
			}
		}
	} else {
		my $res_path = "";
		$res_path = "/data/share/code/trunk/perl/stock/USA/warrant/best_warrant/data/res/$stock.txt";

		open FV, $res_path;
		my $line = ();
		my @arr = ();
		while($line = <FV>) {
			chomp($line);
			my @sub_arr = ();
                        #13124|腾讯摩通五一购A|2014-01-23|2014-12-22|840.000|16.89|4.86|0.119|501|0.375|622.5| 

			#if ($line =~ /(.+?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|/) {
			if ($line =~ /(.+?)\|(.+?)\|(.*?)\|(.*?)\|(.+?)\|(.*?)\|(.*?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|(.+?)\|/) {
				@sub_arr = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);
				my $start_date = $sub_arr[2];
				my $start_time;
				if ($start_date =~ /(\d+)\-(\d+)\-(\d+)/) {
					$start_time = timelocal(0,0,0,$3,$2-1,$1);
				}
				my $end_date = $sub_arr[3];
				my $end_time;
				if ($end_date =~ /(\d+)\-(\d+)\-(\d+)/) {
					$end_time = timelocal(0,0,0,$3,$2-1,$1);
				}
				my $log_date = $sub_arr[7];
				my $log_time;
				if ($log_date =~ /(\d+)\-(\d+)\-(\d+)/) {
					$log_time = timelocal(0,0,0,$3,$2-1,$1);
				}

				my $now_time = time;

				my $ex_date_cnt = int(($end_time-$start_time) / 86400);
				my $to_now_date_cnt = int(($now_time-$start_time) / 86400);
				my $log_date_to_now_date_cnt = int(($now_time-$log_time) / 86400);

				my $rate = sprintf("%.2f", ($sub_arr[10]-$sub_arr[8]) / $sub_arr[8] * 100);
				my $rate_daily = sprintf("%.2f", $rate / ($log_date_to_now_date_cnt + 0.00000001));

				push @sub_arr , $ex_date_cnt;
				push @sub_arr , $to_now_date_cnt;
				push @sub_arr , $ex_date_cnt - $to_now_date_cnt;
				push @sub_arr , $rate;
				push @sub_arr , $rate_daily;

				push @arr, \@sub_arr;
			}
		}

		my @sort_arr = @arr;
		foreach my $idx (0 .. scalar(@sort_arr)-1) {
			my @sub_arr = @{$sort_arr[$idx]};
			my $sort_value;
			if ($order_by >=0 and $order_by <=scalar(@sub_arr)) {
				$sort_value = @sub_arr[$order_by];
			}

			foreach my $idx2 ($idx+1 .. scalar(@sort_arr)-1) {
				my @compare_arr = @{$sort_arr[$idx2]};
				my $compare_value = @compare_arr[$order_by];
				my $swap_flag = 0;
				my $isnum = 0;
				my $compare_gt = 0;
				if ($sort_value =~ /[0-9]+$/) {
					$isnum = 1;
				}
				if ($sort_value =~ /(\d+\-\d+\-\d+)/) {
					$isnum = 0;
				}

				if ($isnum == 1) {
					if ($sort_value > $compare_value)  {
						$compare_gt = 1;
					}
				} else {
					if ($sort_value gt $compare_value)  {
						$compare_gt = 1;
					}
				}

				if (($dir eq "desc" and $compare_gt == 0)
						or ($dir eq "asc" and $compare_gt == 1)
				   ) {
					$swap_flag = 1;
					#print "sort_value=$sort_value,compare_value=$compare_value,swap_flag=$swap_flag<br>";
				}

				if ($swap_flag == 1) {
					my @tmp_arr = @{$sort_arr[$idx]};
					@sort_arr[$idx] = @sort_arr[$idx2];
					@sort_arr[$idx2] = \@tmp_arr;
					@sub_arr = @{$sort_arr[$idx]};
					$sort_value = @sub_arr[$order_by];
				}
			}
		}


		print "<table border=\"1\"><tr><th>0:代码</th><th>1:名称</th><th>2:上市时间</th><th>3:最后交易日</th><th>4:行使价格</th><th>5:杠杆比率</th><th>6:实际杠杆</th><th>7:日志时间</th><th>8:日志价</th><th>9:日志价</th><th>10:当前价</th><th>11:当前价</th><th>12:交易天数</th><th>13:至今天数</th><th>14:剩余天数</th><th>15:至今收益</th><th>16:至今日收益</th></tr>";
		foreach my $idx (0 .. scalar(@sort_arr)-1) {
			my @sub_arr = @{$sort_arr[$idx]};
			print "<tr>";
			foreach my $idx2 (0 .. scalar(@sub_arr)-1) {
				if ($idx2 == 1) {
					print "<td><a href=\"http://hk.warrants.com/home/gb/sgdata/indicator_gb2.cgi?action=pass&code=$sub_arr[0]\" target=\"_blank\">$sub_arr[$idx2]</a></td>";
				} else {
					print "<td>$sub_arr[$idx2]</td>";
				}
			}
			print "</tr>";
		}

	}
	print end_html;
}

main();

